<template>
	<view class="content">
		<g-watermark/>
		<view style="margin: 0 auto" v-if="deptCode">
			<text style="font-weight: bold">{{ emailType == 1 ? '我的邮箱' : '部门邮箱' }}</text>
		</view>
		<view class="content_box" v-for="(item,index) in tabs" :key="index" @click="handleClickItem(item)">
			<view class="content_box_img">
				<image :src="item.icon" alt="" style="width: 60rpx;height: 60rpx;" />
			</view>
			<view class="content_box_label">
				{{item.label}}
			</view>
		</view>
		<view style="margin-top: 20rpx;"  v-if="deptCode&&message_oaGetAgentInfo.isDeptMgr">
			<view style="display: flex;gap:20rpx;font-size: 28rpx;justify-content: center;flex-direction: column;">
				<view style="text-align: center;" v-if="emailType == 2">
					代理部门：{{message_oaGetAgentInfo.agentDeptName || '无'}}
				</view>
				<view  style="display: flex;gap:20rpx;font-size: 28rpx;justify-content: center;">
					<u-button size="medium" type="primary" @click="handleSetProxy(1)"
						style="font-size: 28rpx;" v-if="emailType == 2">设置代理</u-button>
					<u-button size="medium" type="primary" @click="handleSetProxy(2)"
						style="font-size: 28rpx;" v-if="message_oaGetAgentInfo.agentDeptName&&emailType == 2">取消代理</u-button>
					<u-button size="medium" type="primary" @click="handleChangeEmail()">切换邮箱</u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		postFormAction
	} from '@/utils/request.js';
	import in_box_svg from '../static/email/in_box.png';
	import write_svg from '../static/email/write.png';
	import garbage_bin_svg from '../static/email/garbage_bin.png';
	import has_send_svg from '../static/email/has_send.png';
	import draft_svg from '../static/email/draft.png';
	import store from '@/store' // 引入vuex
	import {
		mapState
	} from "vuex";
	export default {
		data() {
			return {
				tabs: [{
						label: '写信',
						value: '1',
						icon: write_svg,
					},
					{
						label: '收件箱',
						value: '2',
						icon: in_box_svg,
					},
					{
						label: '草稿箱',
						value: '3',
						icon: draft_svg,
					},
					{
						label: '发件箱',
						value: '4',
						icon: has_send_svg,
					},
					{
						label: '垃圾箱',
						value: '5',
						icon: garbage_bin_svg,
					},
				],
				emailType: 1,
				message_oaGetAgentInfo: {}, //代理信息
				isDoRefresh: false
			}
		},
		onLoad() {
			this.handleInit()
		},
		//刷页面
		onShow(e) {
			const that = this;
			let pages = getCurrentPages();
			//刷新页面
			let currPage = pages[pages.length - 1];
			if (currPage.isDoRefresh == true) {
				currPage.isDoRefresh = false;
				that.handleInit()
			}
		},
		computed: {
			...mapState(["userInfo"]),
			deptCode(){
				console.log('userInfo',this.userInfo)
				return this.userInfo.deptCode
			},
		},
		methods: {
			handleInit() {
				this.getAgentInfoData()
			},
			handleClickItem(item) {
				switch (item.value) {
					case '1':
						uni.navigateTo({
							url: `/cmn/internalEmail/writebox/index?emailType=${this.emailType}`,
						});
						break
					case '2':
						uni.navigateTo({
							url: `/cmn/internalEmail/inbox/index?emailType=${this.emailType}`,
						});
						break
					case '3':
						uni.navigateTo({
							url: `/cmn/internalEmail/draftbox/index?emailType=${this.emailType}`,
						});
						break
					case '4':
						uni.navigateTo({
							url: `/cmn/internalEmail/outbox/index?emailType=${this.emailType}`,
						});
						break
					case '5':
						uni.navigateTo({
							url: `/cmn/internalEmail/garbageBinbox/index?emailType=${this.emailType}`,
						});
						break
				}
			},
			//获取代理信息
			async getAgentInfoData() {
				const deptCode = this.deptCode
				if (!deptCode) {
					return
				}
				const res = await postFormAction('/message_oa/getAgentInfo', {
					deptCode: deptCode
				})
				this.message_oaGetAgentInfo = res.data || {}
				console.log("this.message_oaGetAgentInfo", this.message_oaGetAgentInfo)
			},
			//设置代理
			handleSetProxy(type) {
				if (type == 1) {
					uni.navigateTo({
						url: `/cmn/internalEmail/common/selectDept?deptCode=${this.deptCode}`,
					});
				} else if (type == 2) {
					const that = this
					uni.showModal({
						title: "提示",
						content: "您确定取消代理么？",
						success: async (res) => {
							if (res.confirm) {
								try {
									uni.showLoading({
										title: '取消中',
										mask: true
									});
									await postFormAction('/message_oa/cancelAgent', {
										deptCode: that.deptCode
									});
									that.getAgentInfoData();
								} catch (e) {} finally {
									uni.hideLoading()
								}
							}
						}
					})
				}
			},
			//切换邮箱
			handleChangeEmail() {
				console.log("emailType", this.emailType)
				if (this.emailType == 1) {
					this.emailType = 2;
				} else {
					this.emailType = 1;
				}
				store.commit('SET_EMAIL_TYPE', this.emailType);
				console.log("handleChangeEmail", this.emailType)
			}

		}
	}
</script>

<style lang="less" scoped>
	.content {
		padding: 20rpx;
		display: flex;
		flex-direction: column;
		gap: 20rpx;

		&_box {
			display: flex;
			align-items: center;
			gap: 10px;

			&_img {}

			&_label {
				flex: 1;
				padding: 10px;
				border-bottom: 1px solid rgba(0, 0, 0, .1);
			}
		}
	}

	::v-deep .u-size-medium {
		padding: 0 40rpx !important;
	}
</style>